<?php
abstract class  CMS_Content_Item_Abstract {
	public $id;
	public $name;
	public $parent_id= 0 ;
	protected $_namespage = 'page';
	protected $_pageModel;
	public function _construc($pageId = null){
		$this->_pageModel = new Model_Page();
		if(null != $pageId){
			$this->loadPageObject(intval($pageId));
		}
	}
	public function _getInnerRow($id = null){
		if($id == null){
			$id = $this->id;
		}
		return $this->_pageModel->find($id)->current();
	}
	// khong hiu cai nay lam
	protected function  _getProperties(){
		$propertyArray = array();
		$class = new Zend_Reflection_Class($this);
		$properties = $class->getProperties();
		foreach ($properties as $property){
			if($property->isPublic())
				$propertyArray[] = $property->getName();
		}
		return $propertyArray;
	}
	public function _callSetterMethod($property, $data){
		$method = Zend_Filter::filterStatic($property,'Word_UnderscoreCamelCase');
		$methodname = '_set'.$method;
		if(method_exists($this, $methodName)){
			return  $this->$methodName($data);
		}
		else{
			return self::NO_SETTER;
		}
	}
	public function loadPageObject($id){
		$this->id = $id;
		$row = $this->getInnerRow();
		if($row){
			if($row->namespace != $this->_namespage){
				throw new Zend_Exception('Unable to cast page type: '.$row->namespace.'to type: '.$this->_namespage);
			}
			$this->name = $row->name;
			$this->parent_id = $row->parent_id;
			$contentNode = new Model_ContentNode();
			$node = $row->findDependentRowset($contentNode);
			if($node){
				$properties = $this->_getProperties();
				foreach ($node as $node){
					$key = $node['node'];
					if(in_array($key,$properties)){
						$value = $this->_callSetterMethod($key,$node);
						if($value === self::NO_SETTER){
							$value = $node['content'];
						}
						$this->$key = $value;
					}
				}
			}
		}else {
			throw new Zend_Exception('unable to load.....');
		}
	}
	public function toArray(){
		$properties = $this->_getProperties();
		foreach ($properties as $property){
			$array[$property] = $this->$property;
			//print_r($array);
		}
		return $array;
	}
	public function save(){		
		if(isset($this->id)){
			$this->_update();
		}else{
			$this->_insert();
		}
	}
	protected function _insert(){
		$this->_pageModel = new Model_Page();
		$pageId = $this->_pageModel->createPage(
		$this->name, $this->_namespage, $this->parent_id);
		$this->id = $pageId;
		$this->_update();
	}
	protected function _update(){
		$data = $this->toArray();
		$this->_pageModel->updatePage($this->id , $data);
	}
	/*
	public function delete(){
		$this->_pageModel = new Model_Page();	
		if(isset($this->id)){
			$this->_pageModel->deletePage($this->id);
		}else{
			throw new Zend_Exception('unable to delete');
		}
	}
	*/
	public function delete($id){
		$this->id = $id;
		$this->_pageModel = new Model_Page();		
		if(isset($this->id)){
			$this->_pageModel->deletePage($this->id);
		}else{
			throw new Zend_Exception('unable to delete');
		}
		
	}
	
}